// $Id: filedistributornaive.cpp 63 2004-10-22 14:33:20Z jonas $
#include "filedistributornaive.h"
#include <stdlib.h>
#include <iostream>
using namespace std;

FileDistributorNaive::FileDistributorNaive(list<Node *> *nodes)
	: FileDistributor(nodes)
{
}

/**
 * Distribute the files onto the nodes in the network. This is a naive way of
 *  doing this. Each file is placed at a single node, and the chosen node is
 *  the one that is closest to the file (freenet distance style).
 */
void FileDistributorNaive::distribute()
{
	int index = 0;
	File *file;

	// Get files one by one from the file handler
	while ((file = FileHandler::getFileByIndex(index++))) {
		// Find the closest node and
		Node *node = getClosestNode(file);

		// Give the file to that node
		if (node) {
			node->storeFile(file, true);
		}
	}
}

/**
 * @deprecated
 * Get the node that is closest (freenet distance style) to the file.
 *
 * @param file The file to compare distance with.
 * @returns The node closest node. Or 0 if there are no files in the file list.
 */
Node *FileDistributorNaive::getClosestNode(File *file)
{
	/// @todo Solve this for correct routing.
	cout << "FileDistributorNaive::getClosestNode>> This is not implemented any more..." << endl;	// DEBUG
	return 0;	// DEBUG
	/*
	// Dont search if there aren't any nodes
	if (m_nodes->empty()) {
		return 0;
	}

	// Begin with the first node
	list<Node *>::iterator closestNode = m_nodes->begin(), currentNode=closestNode;
	int closestDistance = abs((int) ((*closestNode)->getId() - file->m_hash)), currentDistance;

	closestNode++;
	for (; currentNode!=m_nodes->end(); currentNode++) {
		// Calc freenet style distance
		currentDistance = abs((int) ((*currentNode)->getId() - file->m_hash));
		if (currentDistance < closestDistance) {
			closestDistance = currentDistance;
			closestNode = currentNode;
		}
	}

	return *closestNode;
	*/
}
